package ink.lovejinhu.leetcodeMatch;

/**
 * @author jinhu
 * created 2021-07-25 10:52
 */
public class Solution_5824 {

    public String maximumNumber(String num, int[] change) {
        char[] chars = num.toCharArray();
        int[] charsReplice = new int[chars.length];
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            charsReplice[i] = chars[i] - '0';
        }
        for (int i = 0; i < chars.length; i++) {
            if (charsReplice[i] < change[charsReplice[i]]) {
                charsReplice[i] = change[charsReplice[i]];
                int j = i + 1;
                while (j < num.length() && charsReplice[j] <= change[charsReplice[j]]) {
                    charsReplice[j] = change[charsReplice[j]];
                    j++;
                }
                break;
            }

        }
        for (int i = 0; i < charsReplice.length; i++) {
            stringBuilder.append(charsReplice[i]);
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        Solution_5824 solution_5824 = new Solution_5824();
//       "3013"
//[5,4,2,3,5,1,1,6,1,5]
        int[] change = {5, 4, 2, 3, 5, 1, 1, 6, 1, 5};
        solution_5824.maximumNumber("3013", change);
    }
}
